//
//  CYDatePickerView.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/27.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYDatePickerView.h"

@interface CYDatePickerView ()

@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) NSString *dateStr;
@property (strong, nonatomic) NSDate *date;

@property (copy, nonatomic) SelectedDate resultBlock;
@end

@implementation CYDatePickerView

- (void)setMinDate:(NSDate *)minDate {
    _minDate = minDate;
    
    [self setupRangeOfDate];
}

- (void)setMaxDate:(NSDate *)maxDate {
    _maxDate = maxDate;
    
    [self setupRangeOfDate];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupPicker];
        
        self.layer.shadowOffset = CGSizeMake(0, .2);
        self.layer.shadowOpacity = .8;
    }
    return self;
}

- (void)setupPicker {
    self.backgroundColor = [UIColor whiteColor];
    
    //头部下划线
    UIView *topline = [[UIView alloc] initWithFrame:CGRectMake(0, 33, self.width, 3)];
    topline.backgroundColor = CYNaviThemeColor;
    [self addSubview:topline];
    
    //确定按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 60, 3, 29, 29)];
    [self addSubview:btn];
    [btn setBackgroundImage:[UIImage imageNamed:@"ic_done_bluish_24dp"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.dateStr = [self dateFormatter:[NSDate date]];//默认为当前时间
    self.date = [NSDate date];
    
    //日期选择
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 34, self.frame.size.width, 216)];
    self.datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    [self addSubview:self.datePicker];
    
    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];//重点：UIControlEventValueChanged
    
    //设置显示格式
    //默认根据手机本地设置显示为中文还是其他语言
//    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
//    self.datePicker.locale = locale;//内存泄露
    
    //最大最小还款时间
    [self setupRangeOfDate];
}

-(void)dateChanged:(id)sender{
    UIDatePicker *control = (UIDatePicker*)sender;
    NSDate* pickerDate = control.date;
    self.dateStr = [self dateFormatter:pickerDate];
    self.date = pickerDate;
}

/**
 *  设置最大最小显示时间
 */
- (void)setupRangeOfDate {
    //在当前时间加上的时间：格里高利历
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    //设置时间
    [offsetComponents setYear:0];
    [offsetComponents setMonth:0];
    [offsetComponents setDay:0];
    [offsetComponents setHour:0];
    [offsetComponents setMinute:0];
    [offsetComponents setSecond:0];
    //设置最大值时间
    if (!self.maxDate) {
        self.maxDate = [NSDate date];
    }
    NSDate *maxDate = [gregorian dateByAddingComponents:offsetComponents toDate:self.maxDate options:0];
    //设置最小时间
    if (self.minDate) {
        self.datePicker.minimumDate = self.minDate;
    }
    self.datePicker.maximumDate = maxDate;
}

- (NSString *)dateFormatter:(NSDate *)date {
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy-MM-dd"];
    return [pickerFormatter stringFromDate:date];
}

/**
 *  确定按钮
 */
- (void)btnClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(datePickerView:date:dateFormatter:)]) {
        [self.delegate datePickerView:self date:self.date dateFormatter:self.dateStr];
    }
    
    if (self.resultBlock) {
        _resultBlock(self.date, self.dateStr);
    }
}

- (void)selectedDate:(SelectedDate)date {
    self.resultBlock = date;
}

- (void)setFrame:(CGRect)frame {
    frame.size.width = CYScreeSize.width;
    frame.size.height = 240;
    [super setFrame:frame];
}

@end
